package com.greateamd.timetableasy.server.services;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.greateamd.timetableasy.client.services.cursus.CursusService;
import com.greateamd.timetableasy.domain.Cursus;
import com.greateamd.timetableasy.domain.Event;
import com.greateamd.timetableasy.domain.StudyPeriod;
import com.greateamd.timetableasy.domain.exceptions.DBServerSideException;
import com.greateamd.timetableasy.server.dao.CursusDAO;

@Service("cursusService")
public class CursusServiceImpl implements CursusService {

	private CursusDAO cursusDAO;
	public void setCursusDAO(CursusDAO cursusDAO) {
		this.cursusDAO = cursusDAO;
	}
	
	@Override
	public boolean save(Cursus cursus) throws DBServerSideException {
		try {
			cursusDAO.save(cursus);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
		return true;
	}

	@Override
	public Cursus find(int id) throws DBServerSideException{
		try {
			return cursusDAO.getById(id);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Cursus> getAll() throws DBServerSideException {
		try {
			return cursusDAO.getAll();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean remove(Cursus cursus) throws DBServerSideException {
		try {
			cursusDAO.delete(cursus);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean update(Cursus cursus) throws DBServerSideException {
		try {
			cursusDAO.update(cursus);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}
	
	@Override
	public boolean updateSp(StudyPeriod sp) throws DBServerSideException {
		try {
			cursusDAO.updateSp(sp);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Event> getStudyPeriodPlanning(StudyPeriod studyPeriod, Date fromDate, Date toDate) {
		try {
			return cursusDAO.getStudyPeriodPlanning(studyPeriod, fromDate, toDate);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

}
